# 统一头文件搜索路径（库、可执行程序、测试均可包含）
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

# -----------------------------------------------------------------------------
# 1. 把共享源码编译成静态库，业务程序和测试都链接它
# -----------------------------------------------------------------------------
add_library(thread_pool STATIC
    lib/ThreadPool.cpp
)

target_include_directories(thread_pool
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib
)

target_compile_options(thread_pool
    PRIVATE -Wall -Wextra -Wpedantic
)

# -----------------------------------------------------------------------------
# 2. 正常业务可执行程序，使用自己的 main()
# -----------------------------------------------------------------------------
add_executable(thread_pool_app
    main/main.cpp
)

target_link_libraries(thread_pool_app
    PRIVATE thread_pool
)

# -----------------------------------------------------------------------------
# 3. 单元测试配置
#    - 通过 FetchContent 自动拉取 GoogleTest（若本地已有，可改为 find_package）
#    - 测试可执行文件链接 GTest::gtest_main（自带 main 函数）
# -----------------------------------------------------------------------------
# include(FetchContent)
# FetchContent_Declare(
#     googletest
#     GIT_REPOSITORY https://github.com/google/googletest.git
#     GIT_TAG        release-1.14.0
# )
# # 只在第一次配置时下载/编译
# FetchContent_MakeAvailable(googletest)

find_package(GTest)

enable_testing()

add_executable(thread_pool_tests
    tests/ThreadPool_test.cpp
    tests/Thread_test.cpp
    tests/test_main.cpp
)

target_link_libraries(thread_pool_tests
    PRIVATE
        thread_pool       # 复用业务库
        GTest::gtest_main # 使用 gtest 提供的 main()
        pthread           # pthread_cancel 等 POSIX 接口需要
)

include(GoogleTest)
gtest_discover_tests(thread_pool_tests)